using System.Collections.Generic;

namespace Mbs.Elements
{
    class ElementMapRepository : Dictionary<object, IElement>, IElementMapRepository
    {
        private readonly IElementFactoryRepository Factories;

        public ElementMapRepository(IElementFactoryRepository factories)
        {
            Factories = factories;
        }

        public IElement Register(object uiElement)
        {
            IElement result = null;

            if (!TryGetValue(uiElement, out result))
            {
                var factory = Factories.GetFactory(uiElement);
                result = factory.Create(uiElement);

                this[uiElement] = result;
            }
            return result;
        }
    }
}
